var app = getApp();
var Bmob = require('../../utils/bmob.js');

Page({
  data: {
    businessData: null,
    businessImgData: [],
    currentIndex: 1,
    selfLook: false,
    otherLook: false

  },
  onLoad: function (options) {
    if (options.businessId != undefined && options.businessId != '') {
      let self = this;

      var businessImgquery = Bmob.Query('BusinessImg');
      businessImgquery.equalTo("business", '==', options.businessId);
      businessImgquery.include("business");
      businessImgquery.find().then(result => {
        var businessImgData = [];
        for (var i = 0; i < result.length; i++) {
          businessImgData.push(result[i].img.url);
        }
        self.setData({
          businessImgData: businessImgData,
          businessData: result[0].business
        });
        //更新店铺热度
        var businessQuery = Bmob.Query('Business');
        if (app.isNullEmptyOrUndefined(self.data.businessData.store_hot)){
          self.data.businessData.store_hot = 0;//从0开始计算
        }
        
        businessQuery.set("id", self.data.businessData.objectId);
        businessQuery.set("store_hot", self.data.businessData.store_hot + 1);
        businessQuery.save().then(res =>{
          console.log("更新热度成功！");
        }).catch(err =>{
          console.log("更新热度失败！");
        });

        //判断是否是自己的店铺
        if (app.isNotNullEmptyOrUndefined(app.globalData.userBusiness) &&
         app.globalData.userBusiness.id == result[0].business.id) {
          self.setData({
            selfLook: true
          });
        }
        //判断自己是否有店铺
        else if (app.isNullEmptyOrUndefined(app.globalData.userBusiness) || app.globalData.userBusiness.id != result[0].business.id) {
          self.setData({
            otherLook: true
          });
        }

      }).catch(error => {
        console.log(error);
        wx.hideLoading();
      });
    }
  },
  setCurrent: function (e) {  //当前图片索引
    this.setData({
      currentIndex: e.detail.current + 1
    })
  },
  imgPreview: function () { //图片预览
    const imgs = this.data.businessImgData;
    wx.previewImage({
      current: imgs[this.data.currentIndex - 1], // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  getAddress: function (e) {
    // const d = e.currentTarget.dataset;
    // const address = d.address;
    // const latitude = d.latitude;
    // const longitude = d.longitude;
    // wx.openLocation({
    //   latitude: latitude,
    //   longitude: longitude,
    //   scale: 18,
    //   // name: address,
    //   address: address,
    //   success: function (res) {
    //     console.log(res);
    //   },
    //   fail: function (res) {
    //     console.log(res);
    //   },
    //   success: function (res) {
    //     console.log(res);
    //   }
    // })
  },
  editApply: function () {
    wx.navigateTo({
      url: '../apply/apply?businessId=' + app.globalData.userBusiness.objectId,
    });
  },
  businessApply: function (e) {
    app.businessApply(e);
  },
  callPhone: function () {
    var self = this;
    console.log(self.data.businessData);
    var userQuery = Bmob.Query('_User');
    wx.showNavigationBarLoading();
    userQuery.get(self.data.businessData.user.objectId).then(res => {
      wx.hideNavigationBarLoading();
      wx.makePhoneCall({
        phoneNumber: res.mobilePhoneNumber
      });
    }).catch(err => {
      wx.hideNavigationBarLoading();
    });
    
  }
})
